package com.design.pattern.prototype;

import java.io.*;

/**
 * 深copy
 */
public class DeepCopy implements Cloneable {
    /**
     * 深copy
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    public DeepCopy clone() throws CloneNotSupportedException {
        DeepCopy deepCopy=null;
        try(
                ByteArrayOutputStream bos=new ByteArrayOutputStream();
                ObjectOutputStream oos=new ObjectOutputStream(bos);
                ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
                ObjectInputStream ois = new ObjectInputStream(bis);

        ){
            oos.writeObject(this);
            deepCopy=(DeepCopy)  ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return deepCopy;
    }
}
